import {login, logout,checkUserRegister} from '@/api/login';

const userinfo = {
	state: {
		name : '',
		age : ''
	},
	mutations: {  //变动 禁止异步操作
		SET_NAME: (state, name) => {
			state.name = name;
		},
		SET_AGE: (state, age) => {
			state.age = age;
		},
		SET_TOKEN : (state, token) => {
			state.token = token;
		}
	},
	actions: {  //动作 允许异步操作
		Login({commit}, userInfo) {
			const username = userInfo.phone.trim();
			return new Promise((resolve, reject) => {
				login(username, userInfo.password).then(response => {
					const data = response.data;
					//commit('SET_TOKEN', data.token);
					//setToken(response.data.token); //todo 先不设置token
					resolve()
				}).catch(error => {
					reject(error)
				})
			});
		},
		CheckUserRegiter({commit},info){
			return new Promise((resolve, reject) => {
				checkUserRegister( info.phone).then(response => {
					//const data = response.data;
					//commit('SET_TOKEN', data.token);
					//setToken(response.data.token); //todo 先不设置token
					resolve()
				}).catch(error => {
					reject(error)
				})
			});
		}
	}
};

export default userinfo;